import { getToken } from '@/utils/auth';

export default (options: any) => {
  return {
    searchFileds: [
      {
        span: 5,
        field: 'deptId',
        placeholder: '请输入所属科室',
        label: '所属科室',
        tag: 'a-tree-select',
        initValue: '',
        attrs: {
          'data': options.deptTree,
          'treeProps': {
            virtualListProps: {
              height: 200,
            },
          },
          'field-names': {
            key: 'id',
            title: 'label',
          },
          'allow-search': true,
          'allow-clear': true,
          'filter-tree-node': (
            searchValue: string,
            nodeData: { label: string }
          ) => {
            return (
              String(nodeData.label)
                .toLowerCase()
                .indexOf(String(searchValue).toLowerCase()) > -1
            );
          },
        },
      },
      {
        span: 5,
        field: 'nickName',
        placeholder: '请输入患者姓名',
        label: '患者姓名',
        tag: 'a-input',
        initValue: '',
      },
      {
        span: 5,
        field: 'idNumber',
        placeholder: '请输入身份证号',
        label: '身份证号',
        tag: 'a-input',
        initValue: '',
      },
      {
        span: 5,
        field: 'sex',
        placeholder: '请选择性别',
        label: '性别',
        initValue: '',
        tag: 'a-select',
        attrs: {
          options: options.sys_user_sex,
        },
      },
    ],
    tableColumns: [
      {
        dataIndex: 'id',
        title: '序号',
        width: 150,
      },
      {
        dataIndex: 'nickName',
        title: ' 患者姓名',
        width: 150,
      },
      {
        dataIndex: 'sex',
        title: '患者性别',
        slotName: 'sex',
        width: 100,
      },
      {
        dataIndex: 'idNumber',
        title: '身份证号',
        width: 200,
      },
      {
        dataIndex: 'drugAllergy',
        title: '过敏史',
        slotName: 'drugAllergy',
      },
      {
        dataIndex: 'pastMedicalHistory',
        slotName: 'pastMedicalHistory',
        title: '既往病史',
        ellipsis: true,
        tooltip: true,
      },
      {
        dataIndex: 'nowMedicalHistory',
        title: '现病史',
        ellipsis: true,
        tooltip: true,
      },
      {
        dataIndex: 'createTime',
        title: '检查时间',
      },
      {
        dataIndex: 'optional',
        title: '操作',
        slotName: 'optional',
        width: 220,
      },
    ],
    editFields: [
      {
        key: 'userId',
        label: '患者',
        tag: 'a-select',
        attrs: {
          options: options.userList,
          placeholder: '请选择患者',
          allowSearch: true,
        },
        decorator: {
          rules: [{ required: true, message: '请选择患者' }],
        },
      },
      {
        key: 'drugAllergy',
        label: '药物过敏史',
        tag: 'a-checkbox-group',

        attrs: {
          options: options.drug_allergy,
          placeholder: '请选择药物过敏史',
        },
        decorator: {
          rules: [{ required: false, message: '请选择药物过敏史' }],
        },
      },
      {
        key: 'pastMedicalHistory',
        label: '既往病史',
        tag: 'a-checkbox-group',
        attrs: {
          placeholder: `请选择既往病史`,
          options: options.past_medical_history,
        },
        decorator: {
          rules: [{ required: false, message: `请选择既往病史` }],
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
      },
      {
        key: `pastMedicalOther`,
        label: `既往病史-其他`,
        tag: 'a-input',
        relation: 'pastMedicalHistory',
        attrs: {
          placeholder: `请输入其他病史`,
        },
        decorator: {
          rules: [{ required: false, message: `请输入其他病史` }],
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
        show: (value: any, field: any, fields: any) => {
          const key = field.relation;
          const relation = fields?.find((f: { key: any }) => f.key === key);
          const other = relation?.attrs?.options?.find(
            (op: { label: string }) => op.label === '其他'
          );
          const relationValue = value[key] || [];
          if (relationValue?.includes(other?.dictValue)) {
            return true;
          }
          return false;
        },
      },
      // {
      //   key: 'pastMedicalHistory',
      //   label: '既往病史',
      //   tag: 'a-input',
      //   span: 24,
      //   attrs: {
      //     placeholder: '请输入既往病史',
      //   },
      //   formItemAttrs: {
      //     'label-col-props': { span: 4 },
      //     'wrapper-col-props': { span: 20 },
      //   },
      //   decorator: {
      //     rules: [{ required: true, message: '请输入既往病史' }],
      //   },
      // },
      {
        key: 'nowMedicalHistory',
        label: '现病史',
        tag: 'a-textarea',
        span: 24,
        attrs: {
          placeholder: '请输入现病史',
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
        decorator: {
          rules: [{ required: true, message: '请输入现病史' }],
        },
      },
      {
        key: 'operationOrNot',
        label: '是否做过手术',
        tag: 'a-radio-group',
        span: 24,
        attrs: {
          placeholder: '请选择是否做过手术',
          options: [
            { value: 2, label: '否' },
            { value: 1, label: '是' },
          ],
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
        decorator: {
          rules: [{ required: true, message: '请选择是否做过手术' }],
        },
      },
      {
        key: `operationTime`,
        label: `手术时间`,
        tag: 'a-date-picker',
        relation: 'operationOrNot',
        attrs: {
          placeholder: `请选择上次手术时间`,
          style: 'width: 100% !important;',
          showTime: true,
        },
        decorator: {
          rules: [{ required: false, message: `请选择上次手术时间` }],
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
        show: (value: any, field: any, fields: any) => {
          const key = field.relation;
          const relation = fields?.find((f: { key: any }) => f.key === key);
          const other = relation?.attrs?.options?.find(
            (op: { value: number }) => op.value === 1
          );
          const relationValue = value[key] || [];
          if (relationValue === other?.value) {
            return true;
          }
          return false;
        },
      },
      {
        key: `operationType`,
        label: `手术样式`,
        tag: 'a-input',
        relation: 'operationOrNot',
        attrs: {
          placeholder: `请输入上次手术样式`,
        },
        decorator: {
          rules: [{ required: false, message: `请输入上次手术样式` }],
        },
        formItemAttrs: {
          'label-col-props': { span: 4 },
          'wrapper-col-props': { span: 20 },
        },
        show: (value: any, field: any, fields: any) => {
          const key = field.relation;
          const relation = fields?.find((f: { key: any }) => f.key === key);
          const other = relation?.attrs?.options?.find(
            (op: { value: number }) => op.value === 1
          );
          const relationValue = value[key] || [];
          if (relationValue === other?.value) {
            return true;
          }
          return false;
        },
      },
      {
        key: 'pictureAddress',
        label: '术前检查信息',
        tag: 'XUpload',
        attrs: {
          config: {
            'list-type': 'picture-card',
            'action': `${import.meta.env.VITE_API_BASE_URL}${
              import.meta.env.VITE_UPLOAD_URL
            }`,
            'headers': {
              Authorization: `Bearer ${getToken()}`,
            },
            'imagePreview': true,
            'limit': 9,
            'accept': ['image/*'],
          },
        },
        decorator: {
          rules: [
            { required: false, message: '请上传术前检查信息' },
            {
              validator: (value: any, cb: (arg0: string) => void) => {
                const inUpload = value?.find(
                  (item: { status: string }) => item.status !== 'done'
                );
                if (inUpload?.status === 'uploading') {
                  cb('文件上传中！');
                } else if (inUpload?.status === 'error') {
                  cb('文件上传错误，请重试！');
                } else if (inUpload?.status === 'init') {
                  cb('文件未上传！');
                }
              },
            },
          ],
        },
      },
      {
        key: 'formAddress',
        label: '检查表格',
        tag: 'XUpload',

        attrs: {
          config: {
            action: `${import.meta.env.VITE_API_BASE_URL}${
              import.meta.env.VITE_UPLOAD_URL
            }`,
            headers: {
              Authorization: `Bearer ${getToken()}`,
            },
          },
        },
        decorator: {
          rules: [
            { required: false, message: '请上传检查表格' },
            {
              validator: (value: any, cb: (arg0: string) => void) => {
                const inUpload = value?.find(
                  (item: { status: string }) => item.status !== 'done'
                );
                if (inUpload?.status === 'uploading') {
                  cb('文件上传中！');
                } else if (inUpload?.status === 'error') {
                  cb('文件上传错误，请重试！');
                } else if (inUpload?.status === 'init') {
                  cb('文件未上传！');
                }
              },
            },
          ],
        },
      },
      {
        key: 'remark',
        label: '备注',
        tag: 'a-input',
        attrs: {
          placeholder: '请输入备注',
        },
        decorator: {
          rules: [{ required: false, message: '请输入备注' }],
        },
      },
    ],
  };
};
